<template>
  <el-card class="content">
    <el-row>
      <el-col :span="4">
        <span>巡查点：</span>
        <el-input
          placeholder="请输入巡查点"
          size="mini"
          style="width: 200px"
          v-model="query.position"
          clearable
        ></el-input>
      </el-col>
      <el-col :span="9">
        <span>开始时间：</span>
        <el-date-picker
          type="datetime"
          placeholder="选择开始日期"
          default-time="00:00:00"
          size="mini"
          v-model="startTime"
          :picker-options="pickerOptions0"
          clearable
        ></el-date-picker>
        <span>结束时间：</span>
        <el-date-picker
          type="datetime"
          placeholder="选择结束日期"
          default-time="00:00:00"
          size="mini"
          v-model="endTime"
          :picker-options="pickerOptions0"
          clearable
        ></el-date-picker>
      </el-col>
      <el-col :span="4">
        <el-button
          class="black-button"
          size="mini"
          type="primary"
          @click="getSearch"
          >查询</el-button
        >
        <el-button
          class="black-button"
          type="primary"
          size="mini"
          @click="selectAllConfigurateOptions"
          >配置</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="otherInspectList"
      stripe
      size="small"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      class="el-table-box"
    >
      <el-table-column
        prop="position"
        label="巡查点"
        align="center"
        width="150px"
      >
      </el-table-column>
      <el-table-column
        prop="inspectTime"
        label="巡查时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inspectUserName"
        label="巡查人"
        align="center"
      ></el-table-column>
      <el-table-column prop="shiftLevel" label="班别" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.shiftLevel == '1'">早</span>
          <span v-if="scope.row.shiftLevel == '2'">中</span>
          <span v-if="scope.row.shiftLevel == '3'">晚</span>
        </template>
      </el-table-column>
      <el-table-column label="配置项记录" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="configurationInfo(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 15]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
    <!-- 配置表单 -->
    <el-dialog title="配置" :visible.sync="addDialogVisible" width="30%">
      <el-form
        id="configurationForm"
        :model="configurationForm"
        ref="configurationForm"
        size="small"
      >
        <el-form-item prop="servin">
          <el-button
            type="primary"
            @click="addRow(configurationForm.configurationList)"
            style="margin-bottom: 20px"
            >新增</el-button
          >
            <el-table border :data="configurationForm.configurationList" style="width:100%;display: block;max-height: 500px;overflow-y: auto;">
              <el-table-column prop="optionsName" label="字段名(英文)">
                <template scope="scope">
                  <el-input
                    size="mini"
                    v-model="scope.row.optionsName"
                    placeholder="请输入"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="desc" label="描述(中文)">
                <template scope="scope">
                  <el-input
                    size="mini"
                    v-model="scope.row.desc"
                    placeholder="请输入"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column label="操作" style="width:50px">
                <template slot-scope="scope">
                  <el-button
                    @click="deleteRow(scope.$index,configurationForm.configurationList )"
                    size="small"
                    type="danger"
                    >移除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
        </el-form-item>
        <el-form-item class="buttonfrom" style="text-align: center">
          <el-button type="primary" @click="insertConfigurateOptions"
            >确 定</el-button
          >
          <el-button @click="addDialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 配置详情 -->
    <el-dialog title="配置项记录详情" :visible.sync="dialogVisible" width="30%">
      <el-table :data="configurationInfoList" stripe style="width: 100%;display: block;max-height: 60vh;overflow-y: auto;">
        <el-table-column prop="optionsName" label="配置选项"> </el-table-column>
        <el-table-column prop="optionsValue" label="配置值"> </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>
<script>
import {
  getOtherInspectByPage,
  insertConfigurateOptions,
  selectAllConfigurateOptions,
} from "../../../api/safety-management-api";
import moment from "moment";
export default {
  name: "OtherInspections",
  data() {
    return {
      endTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      startTime: moment().subtract(1, "months").format("YYYY-MM-DD 00:00:00"),
      pickerOptions0: {
        disabledDate(time) {
          return time.getTime() > Date.now(); //如果没有后面的-8.64e6就是不可以选择今天的
        },
      },
      query: {
        page: 1,
        pagesize: 10,
        position: "",
        id:null
      },
      total: null,
      // 存放所有交接班信息列表数据
      otherInspectList: [],
      configurationInfoList: [],
      addDialogVisible: false,
      configurationForm: {
        configurationList: [],
      },
      dialogVisible: false,
    };
  },
  created() {
    this.getOtherInspectByPage();
    this.addconfigurateOptions();
  },
  methods: {
    async getOtherInspectByPage() {
      const res = await getOtherInspectByPage({
        pageCount: this.query.page,
        pageSize: this.query.pagesize,
        unitId: sessionStorage.getItem("unitId"),
      });
      console.log(res);
      if (res.resultCode == "200") {
        this.otherInspectList = res.parameter.list;
        this.total = res.parameter.total;
      } else {
        this.$message({
          message: "暂无数据",
          offset: 400,
          duration: 1000,
        });
        this.otherInspectList = [];
      }
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(val) {
      this.query.pagesize = val;
      this.getOtherInspectByPage();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(val) {
      this.query.page = val;
      this.getOtherInspectByPage();
    },
    // 查看详情
    async getSearch() {
      let queryData = {
        startTime: moment(this.startTime).format("YYYY-MM-DD 00:00:00"),
        endTime: moment(this.endTime).format("YYYY-MM-DD HH:mm:ss"),
        timeType: this.timeType,
      };
      const res = await getOtherInspectByPage({
        startTime: queryData.startTime,
        endTime: queryData.endTime,
        pageCount: this.query.page,
        pageSize: this.query.pagesize,
        position: this.query.position,
        unitId: sessionStorage.getItem("unitId"),
      });
      if (res.resultCode == "200") {
        this.$message({
          type: "success",
          message: "成功查询到数据",
          offset: 400,
          duration: 1000,
        });
        this.otherInspectList = res.parameter.list;
        this.total = res.parameter.total;
      } else {
        this.$message({
          message: "暂无数据",
          offset: 400,
          duration: 1000,
        });
        this.otherInspectList = [];
      }
    },
    // 配置项记录详情
    configurationInfo(row) {
      this.configurationInfoList = [];
      if (row.beachTopElevation !== null) {
        let ele = row.optionsValue.split(";");
        for (let i = 0; i < ele.length; i++) {
          let e = ele[i].split(":");
          let obj = {};
          obj.optionsName = e[0];
          obj.optionsValue = e[1];
          this.configurationInfoList.push(obj);
        }
      } else {
        this.configurationInfoList = [];
      }
      this.dialogVisible = true;
    },
    deleteRow(index, rows) {
      //删除改行
      rows.splice(index, 1);
    },
    addRow(tableData, event) {
      tableData.push({ id: "", optionsName: "", desc: "" });
    },
    addconfigurateOptions() {
      this.configurationForm.configurationList.push({
        id: "",
        optionsName: "",
        desc: "",
      });
    },
    async insertConfigurateOptions() {
      let dataList = this.configurationForm.configurationList;
      this.optionsData = configurateOptions(dataList);
      this.configurateOptions = [];
      this.id=[]
      for (let i = 0; i < this.optionsData.length; i++) {
        let ele = this.optionsData[i];
        let configurateOptions = ele.returnStr;
        let id = ele.id;
        this.configurateOptions.push(configurateOptions);
        this.id.push(id);
      }
        await insertConfigurateOptions({
        configurateOptions: this.configurateOptions.join(";"),
        id:this.query.id
      }).then((res) => {
        if (res.resultCode == "200") {
          this.$message({
            type: "success",
            message: "成功新增配置项",
            offset: 400,
            duration: 1000,
          });
        } else {
          this.$message({
            type: "error",
            message: "新增配置项失败",
            offset: 400,
            duration: 1000,
          });
        }
      });
      this.addDialogVisible = false;
    },
    // 获取配置项名
    async selectAllConfigurateOptions() {
      this.configurationForm.configurationList = [];
      const res = await selectAllConfigurateOptions();
      let allConfigurateOptions = res.parameter;
      if (allConfigurateOptions !== null) {
        for (let i = 0; i < allConfigurateOptions.length; i++) {
          let element = allConfigurateOptions[i];
          if (element.configurateOptions !== null) {
             this.query.id = element.id;
            let ele = element.configurateOptions.split(";");
            for (let i = 0; i < ele.length; i++) {
              let e = ele[i].split(":");
              let obj = {};
              obj.optionsName = e[0];
              obj.desc = e[1];
              this.configurationForm.configurationList.push(obj);
            }
          }
        }
        this.addDialogVisible = true;
      } else {
        // this.configurationForm.configurationList = [];
        this.addDialogVisible = true;
      }
      this.addDialogVisible = true;
    },
  },
};
function configurateOptions(dataList) {
  var configurateOptions = [];
  for (let i = 0; i < dataList.length; i++) {
    let optionsName = dataList[i].optionsName;
    let desc = dataList[i].desc;
    let obj = {};
    let ele = optionsName + ":" + desc;
    obj.returnStr = ele;
    obj.id = dataList[i].id;
    configurateOptions.push(obj);
  }
  return configurateOptions;
}
</script>
<style scoped>
.content {
  margin-right: 5px;
  height: 87vh;
}
.list {
  height: 83vh;
}
.el-table-box {
  margin-top: 20px;
  margin-bottom: 20px;
  display: block;
  height: 73vh;
  overflow-y: auto;
}
.el-table::before {
  z-index: inherit;
}
table {
  text-align: center;
}
</style>